<template>
  <BasicModal v-bind="$attrs" @register="registerModal" destroyOnClose :title="title" :width="896" @ok="handleSubmit">
      <BasicForm @register="registerForm"/>
  </BasicModal>
</template>

<script lang="ts" setup>
    import {ref, computed, unref,onMounted} from 'vue';
    import {BasicModal, useModalInner} from '/@/components/Modal';
    import {BasicForm, useForm} from '/@/components/Form/index';
    import {formSchema} from '../DbReceipt.data';
    import {saveOrUpdate} from '../DbReceipt.api';
    import {useUserStore} from "/@/store/modules/user";
    const userStore = useUserStore();
    // Emits声明
    const emit = defineEmits(['register','success']);
    const isUpdate = ref(true);
    //表单配置
    const [registerForm, {setProps,resetFields, setFieldsValue, validate,updateSchema,getFieldsValue}] = useForm({
        labelWidth: 150,
        schemas: formSchema,
        showActionButtonGroup: false,
        baseColProps: {span: 12}
    });
    function resetSomeSchema(){
      updateSchema([{field:"owner2",dynamicDisabled:false},
        {field:"state2",dynamicDisabled:false},
        {field:"dealperson",dynamicDisabled:false},
        {field:"status",dynamicDisabled:false},
        {field:"receiptnum",dynamicDisabled:false},]);
      updateSchema([{field:"owner2",componentProps:{
        onChange:(e)=>{}
        }},{field:"dealperson",componentProps:{
          onChange:(e)=>{}
        }}]);
    }
    //表单赋值
    const [registerModal, {setModalProps, closeModal}] = useModalInner(async (data) => {
        //重置表单
        await resetFields();
        setModalProps({confirmLoading: false,showCancelBtn:!!data?.showFooter,showOkBtn:!!data?.showFooter});
        isUpdate.value = !!data?.isUpdate;
      let login = userStore.userInfo.username;
      resetSomeSchema();
        if (unref(isUpdate)) {
            //表单赋值
            await setFieldsValue({
                ...data.record,
            });
          updateSchema({field:"receiptnum",dynamicDisabled:true});
          if(data.record.owner2 && (login!=data.record.owner1 || (data.record.state2 && data.record.state2!="已入库"))){
            updateSchema({field:"owner2",dynamicDisabled:true});
          }
          if(data.record.owner1){
            updateSchema({field:"owner1",dynamicDisabled:true});
          }
          let cu = (data.record.owner2&&data.record.owner2!="")?data.record.owner2:data.record.owner1;
          if(data.record.dealperson && (login!=cu || (data.record.status && data.record.status!="已分配"))){
            updateSchema({field:"dealperson",dynamicDisabled:true});
          }
          if(data.record.dealperson && !data.record.owner2){
            updateSchema([{field:"owner2",dynamicDisabled:true},{field:"state2",dynamicDisabled:true}]);
          }



          if(login != data.record.owner1 && data.record.owner1){
            updateSchema({field:"state1",dynamicDisabled:true});
          }
          if(login != data.record.owner2 && data.record.owner2){
            updateSchema([{field:"state2",dynamicDisabled:true}]);
            updateSchema([{field:"dealperson",dynamicDisabled:true},{field:"status",dynamicDisabled:true}]);
          }
          if(login != data.record.dealperson && data.record.dealperson){
            updateSchema([{field:"status",dynamicDisabled:true}]);
          }

        }else{
          console.log(userStore)
          setFieldsValue({
            owner1:userStore.userInfo.username,
            state1:"已入库",
          });
        }
      if(!data.record.dealperson || login!=data.record.dealperson){
        updateSchema([{field:"enterprise",dynamicDisabled:true},
          {field:"enterprise",dynamicDisabled:true},
          {field:"payunit",dynamicDisabled:true},
          {field:"billcontent",dynamicDisabled:true},
          {field:"phone",dynamicDisabled:true},
          {field:"type",dynamicDisabled:true},
          {field:"bill",dynamicDisabled:true},
          {field:"receivedate",dynamicDisabled:true},
          {field:"status",dynamicDisabled:true},]);
      }
      if(login==data.record.dealperson){
        updateSchema([{field:"enterprise",dynamicDisabled:false},
          {field:"enterprise",dynamicDisabled:false},
          {field:"payunit",dynamicDisabled:false},
          {field:"billcontent",dynamicDisabled:false},
          {field:"phone",dynamicDisabled:false},
          {field:"type",dynamicDisabled:false},
          {field:"bill",dynamicDisabled:false},
          {field:"receivedate",dynamicDisabled:false},
          {field:"status",dynamicDisabled:false},]);
      }






      updateSchema({field:"owner2",
        componentProps:{
          onChange:(e)=>{
            const fvalue = getFieldsValue();
            const fvalue1 = getFieldsValue();
            console.log(fvalue1)
            console.log(e)
            if(e.length!=0) {
              console.log(e);
              if(fvalue.state1=='已入库'){
                setFieldsValue({state1:"已分发"});
              }
              console.log("t1")
              setFieldsValue({state2: "已入库"});
           //   updateSchema([{field: "state2", dynamicDisabled: true}, {field: "dealperson", dynamicDisabled: true},{field: "status", dynamicDisabled: true}]);
            }else{
              console.log("t2")
              if(fvalue.dealperson){
                return;
              }
              setFieldsValue({state2: null,state1:"已入库"});
             // updateSchema([{field: "state2", dynamicDisabled: false}, {field: "dealperson", dynamicDisabled: false},{field: "status", dynamicDisabled: false}]);
            }
          }}});

      updateSchema({
        field: "dealperson",
        componentProps: {
          onChange:(e)=>{
            const fvalue = getFieldsValue();
            if(e.length!=0) {
              console.log("t3")
              if(fvalue.owner2){
                if(fvalue.state2=='已入库') {
                  setFieldsValue({state2: "已分发"})
                }
              }else{
                console.log("t4")
                if(fvalue.owner1) {
                //  updateSchema([{field: "owner2", dynamicDisabled: true},{field: "state2", dynamicDisabled: true}]);
                  if(fvalue.state1=='已入库') {
                    setFieldsValue({state1: "已分发"})
                  }
                }
              }
              //   updateSchema([{field: "owner2", dynamicDisabled: true},{field: "state2", dynamicDisabled: true}]);
            }else{
              console.log(fvalue)
              if(fvalue.owner2){
                console.log("t5")
                setFieldsValue({state2:"已入库"})
              }else{
                if(fvalue.owner1) {
                  console.log("t6")
                  setFieldsValue({state1: "已入库"})
                 // updateSchema([{field: "owner2", dynamicDisabled: false},{field: "state2", dynamicDisabled: false}]);
                }
              }
              // updateSchema([{field: "owner2", dynamicDisabled: false},{field: "state2", dynamicDisabled: false}]);
            }
          }
        }
      });



        // 隐藏底部时禁用整个表单
       setProps({ disabled: !data?.showFooter })
    });











    //设置标题
    const title = computed(() => (!unref(isUpdate) ? '新增' : '编辑'));
    //表单提交事件
    async function handleSubmit(v) {
        try {
            let values = await validate();

            console.log(values)
           if(values.owner2=="")
             values.owner2 = null;
           if(values.dealperson=="")
             values.dealperson = null;

            setModalProps({confirmLoading: true});
            //提交表单
            await saveOrUpdate(values, isUpdate.value);
            //关闭弹窗
            closeModal();
            //刷新列表
            emit('success');
        } finally {
            setModalProps({confirmLoading: false});
        }
    }
</script>

<style lang="less" scoped>
	/** 时间和数字输入框样式 */
  :deep(.ant-input-number){
		width: 100%
	}

	:deep(.ant-calendar-picker){
		width: 100%
	}
</style>
